import Koa from "koa"; //https://www.npmjs.com/package/koa
import controllerInit from "./controllers/controllerInit.js";
import router from "koa-simple-router"; //https://www.npmjs.com/package/koa-simple-router
import config from "./config";
import log4js from "log4js"; //https://www.npmjs.com/package/log4js
import errorHandler from "./middwares/errorHandler.js";
import serve from "koa-static";
import co from "co"; //https://www.npmjs.com/package/co
import render from "koa-swig" //https://www.npmjs.com/package/koa-swig
const app = new Koa();

//koa v2.0需要co模块
app.context.render = co.wrap(render({
    root: config.viewDir,
    autoescape: true,
    cache: 'memory', // disable, set to false
    varControls:["[[","]]"], //{{}}如果被占用，则把后端的数据渲染改成[[变量]]。
    ext: 'html',
}));

//日志配置
log4js.configure({
    appenders: { cheese: { type: 'file', filename: `${__dirname}/logs/error.log`} },
    categories: { default: { appenders: ['cheese'], level: 'error' } }  //配置打印日志级别,值分别为：trace、debug、info、warn、error、fatal
});
const logger = log4js.getLogger('cheese');

//错误处理中心
errorHandler.error(app,logger);

//集中处理所有路由
controllerInit.getAllRouters(app,router);

//静态资源目录管理
app.use(serve(config.staticDir));


app.listen(config.port,()=>{
    console.info(`node-cli listening on ${config.port}`);
    // logger.error(`node-cli listening on ${config.port}`);
});
